package org.mindrive.system.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mindrive.system.collection.system.UserEntity;
import org.mindrive.system.collection.system.UserRoleEntity;
import org.mindrive.system.dao.mapper.UserMapper;
import org.mindrive.system.dao.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author houwei
 * @since 2021-10-21
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, UserEntity> {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;


    public UserEntity getByUserId(String userId) {
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.eq("user_id", userId);
        UserEntity user = userMapper.selectOne(query);
        return user;
    }

    public String getUserName(String userId) {
        String userName = "";
        if (userId == null || "".equals(userId)) {
            return userName;
        }
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.eq("user_id", userId);
        UserEntity user = userMapper.selectOne(query);
        if (user != null) {
            userName = user.getUserName();
        }
        return userName;

    }

    public UserEntity getUserByUserId(String userId) {
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.eq("user_id", userId);
        final List<UserEntity> list = list(query);
        if (list.size() > 0){
            return list.get(0);
        }else {
            return new UserEntity();
        }
    }

    public int getCountByUserId(String userId) {
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.eq("user_id", userId);
        int count = userMapper.selectCount(query);
        return count;
    }

    public List<String> getRoleIds(String userId) {
        QueryWrapper<UserRoleEntity> query = new QueryWrapper<UserRoleEntity>();
        query.eq("user_id", userId);
        List<UserRoleEntity> roles = userRoleMapper.selectList(query);
        List<String> roleIds = roles.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
        return roleIds;
    }

    public void deleteUserRole(String userId) {
        QueryWrapper<UserRoleEntity> query = new QueryWrapper<UserRoleEntity>();
        query.eq("user_id", userId);
        userRoleMapper.delete(query);
    }

    public int getCountByTelephoneNumber(String id, String telephoneNumber) {
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.ne("id", id).eq("phone_number", telephoneNumber);
        int count = userMapper.selectCount(query);
        return count;
    }

    public int getCountByTelephoneNumber(String telephoneNumber) {
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.eq("phone_number", telephoneNumber);
        int count = userMapper.selectCount(query);
        return count;
    }

    public int getCountByEMail(String id, String email) {
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.ne("id", id).eq("email", email);
        int count = userMapper.selectCount(query);
        return count;
    }

    public int getCountByEMail(String email) {
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.eq("email", email);
        int count = userMapper.selectCount(query);
        return count;
    }


}